<template>
    <el-container class="home">
      <!-- 头部 -->
      <el-header>
        <div>
          <img src="@/assets/logo.png" class="logo"/>
          <span>毕业设计论文管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <!-- 主体 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse?'65px':'150px'">
          <div class="toggle" @click="toggle">{{ isCollapse ? '||&gt;' : '||| &lt;' }}</div>
          <el-menu background-color="#333744"
            text-color="#FFF"
            unique-opened
            router
            :collapse="isCollapse"
            :collapse-transition="false"
            :default-active="activePath"
            active-text-color="#409EFF">
            <!-- 一级菜单 -->
            <el-submenu :index="''+item.id" v-for="item in menus" :key="item.id">
              <!-- 一级菜单模板 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="item.icon || 'el-icon-location'"></i>
                <!-- 文本 -->
                <span class="mgleft">{{ item.name }}</span>
              </template>

              <!-- 二级菜单 -->
              <el-menu-item :index="subItem.path"
              v-for="subItem in item.children"
              :key="item.id+'-'+subItem.id"
              @click="saveNavState(subItem.path)">
                <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本 -->
                  <span>{{ subItem.name }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 内容区 -->
        <el-main>
          <router-view>welcome</router-view>
        </el-main>
      </el-container>
    </el-container>
</template>

<script>
import Manager from '@/utils/authority/manager'

export default {
  data () {
    return {
      role: '',
      isCollapse: false
    }
  },
  computed: {
    menus () {
      return Manager
    },
    activePath () {
      return window.sessionStorage.getItem('activePath')
    }
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getRole () {
      const { data } = await this.$http.get('/api/user/info')
      if (!data || !data.success) return this.$message.error(data.msg)
      this.role = data.role
    },
    toggle () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    }
  },
  created () {
    this.getRole()
  }
}
</script>

<style lang="less" scoped>
.home {
  height: 100%;
}
.logo {
  height: 60px;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #FFF;
  font-size: 20px;
  > div {
      display: flex;
      align-items: center;
    }
    span {
      margin-left: 15px;
    }
}
.el-aside {
  background-color: #373d41;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.toggle {
  background-color: #4A5064;
  font-size: 16px;
  line-height: 24px;
  color: #FFF;
  text-align: center;
  letter-spacing: 0.1em;
  word-spacing: 4.7em;
  cursor: pointer;
}
.mgleft {
  margin-left: 5px;
}
</style>
